package com.hustzp.com.xichuangzhu.utils;

import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

    private AnimateFirstDisplayListener() {
    }

    public static AnimateFirstDisplayListener instance;

    static {
        instance = new AnimateFirstDisplayListener();
    }

    List<String> displayedImages = Collections
            .synchronizedList(new LinkedList<String>());

    @Override
    public void onLoadingStarted(String imageUri, View view) {
        ImageView iv = (ImageView) view;
        iv.setScaleType(ScaleType.CENTER_CROP);
    }

    @Override
    public void onLoadingFailed(String imageUri, View view,
                                FailReason failReason) {
        ImageView iv = (ImageView) view;
        if (iv != null)
            iv.setScaleType(ScaleType.CENTER_CROP);
    }

    @Override
    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
        if (loadedImage != null) {
            ImageView iv = (ImageView) view;
            iv.setScaleType(ScaleType.FIT_XY);
            boolean firstDisplay = !displayedImages.contains(imageUri);
            if (firstDisplay) {
                FadeInBitmapDisplayer.animate(iv, 500);
                displayedImages.add(imageUri);
            }
        }
    }
}